<template>
  <view style="height: 100%">
    <view v-if="details.length" v-for="item in details">
      <u-cell-group :title="item.path">
        <u-cell-item :title="items.jobName" value="职业详情"
                     @click="toDetail(items.id)"
                     v-for="items in item.data"></u-cell-item>
      </u-cell-group>
      <view class="empty" style="height: 10upx;background: #f7f7f7"></view>
    </view>
    <view v-if="!details.length" class="u-flex flex-center u-flex-col" style="height: 100%">
      <u-empty text="暂无数据" mode="list"></u-empty>
    </view>

  </view>
</template>

<script>
  import {request} from "../../util/api";
  import {getCurrentFullPath} from "../../util/util";

  export default {
    onLoad(option) {
      uni.setNavigationBarTitle({
        title: option.n || ''
      });
      if (option.n) {
        this.getDetail(option.n)
      }
      if (option.s) {
        uni.setNavigationBarTitle({
          title:  `【${option.k}】搜索结果` || ''
        });
        this.search(option.k)
      }
    },

    data() {
      return {
        details: []
      }
    },

    methods: {
      getDetail(detail) {
        request({
          url: '/system/wx/CompassJobInfo/getJobPathList/' + detail,
          token: true
        }).then(res => {
          this.groupByPath(res.data, 'jobPath')
        })
      },

      search(value) {
        request({
          url: '/system/wx/CompassJobInfo/list/' + value,
          token: true
        }).then(res => {
          this.groupByPath(res.data, 'jobPath')
        })
      },

      groupByPath(arr, key) {
        let map = {}
        for (let i = 0; i < arr.length; i++) {
          let ai = arr[i]
          if (!map[ai[key]]) {
            map[ai[key]] = [ai]
          } else {
            map[ai[key]].push(ai)
          }
        }
        let res = []
        Object.keys(map).forEach(keys => {
          res.push({
            path: keys,
            data: map[keys],
          })
        })
        this.details = res
      },

      toDetail(id) {
        this.$u.route({
          url: '/pages/explore/job_detail?id=' + id,
        })
      }
    },
    onShareAppMessage(res) {
      return {
        path: getCurrentFullPath()
      }
    }
  }

</script>

<style scoped>

</style>
